// pages/my/myWallet/myWallet.js
const app = getApp();
const utils = require('../../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isAgree:true,
    //是否满足可提现
    isWallet:false,
    isClick:true,
    money:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },
  //是否同意协议
  isAgress:function (){
    let isAgress = this.data.isAgree;
    let money = this.data.money;
    console.log(money)
    if (isAgress) {
      this.setData({
        isAgree:false
      })
    }else{
      this.setData({
        isAgree:true
      })
    }
    if (isAgress || money == '') {
      this.setData({
        isWallet:false
      })
    }else{
      this.setData({
        isWallet:true
      })
    }
  },
  getMoney:function(){
    utils.requestUrl({
      url:'/niu/apiv3/xiandanv3_mymoneyrmb.php?method=total',
      params: {
        markid:wx.getStorageSync('markId')
      }
    }).then((res) =>{
      console.log(res.data);
      let data = res.data.data;
      this.setData({
        cannowout:data.cannowout,
        canout:data.canout
      })
    })
  },
  //点击获取所有余额
  allMoney:function (){
    let isAgress = this.data.isAgree;
    if (isAgress) {
      this.setData({
        isWallet:true,
        money:this.data.cannowout / 100
      })
    }else{
      this.setData({
        isWallet:false,
        money:this.data.cannowout / 100
      })
    }
   
  },
  bindinput:function (e){
    var isWallet;
    if (e.detail.value >= 1) {
      isWallet=true
    }else{
      isWallet=false
    }
    this.setData({
      money:e.detail.value,
      isWallet:isWallet
    })
  },
  //提现
  isWallet:function (){
    var isWallet = this.data.isWallet;
    var isClick =  this.data.isClick;
    if (isWallet && isClick) {
      this.setData({
        isClick:false
      })
      let money = this.data.money;
      let canout = this.data.cannowout;
      
      if (money>canout) {
        utils.toast('提现金额不能大于当前可提现金额');
        return
      }
      if (money > 1000) {
        utils.toast('提现金额不能大于1000元');
        return
      }
      let paycount = money * 100;
      console.log(paycount)
      
      utils.requestUrl({
        url:'/niu/wxpay/wxpay_seller.php?paytype=pickupmoney',
        params: {
          markid:wx.getStorageSync('markId'),
          paycount:paycount,
          openid:wx.getStorageSync('openId')
        }
      }).then((res) =>{
        let data = res.data;
        console.log(data);
        if (data.resultcode == 0) {
          this.getMoney();
          this.setData({
            money:'',
            isClick:true,
            isWallet:false
          })
        }else{
          this.setData({
            isClick:true
          })
          utils.toast(data.msg)
        }
      })
    }
  },
  goToRecord:function () {
    wx.navigateTo({
      url: '/pages/my/myTradingRecord/myTradingRecord?type=0',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //判断是否登录
    if(wx.getStorageSync('markId') == ''){
      wx.navigateTo({
        url: '/pages/login/login',
      })
    }else{
      this.getMoney();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
})